<?php
/**
 * Created by PhpStorm.
 * User: dreamy
 * Date: 2019-05-25
 * Time: 11:16
 */
namespace common\services;

use common\models\TallyProject;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

/**
 * Class TallyProjectService
 * @package common\services
 * 项目管理
 */
class TallyProjectService extends BaseService
{
    /**
     * 缓存存储KEY
     */
    const CACHE_BUCKET = 'TallyProject:';

    /**
     * 缓存有效期
     */
    const CACHE_TIMEOUT = 3600;

    /**
     * @param $uid
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getListByUid($uid,$condition=[])
    {
        $where = [
            'uid'   => $uid,
        ];
        if(ArrayHelper::getValue($condition,'type')){
            $where['type'] = $condition['type'] ; // 收入or支出
        }
        if(ArrayHelper::getValue($condition,'tally_id')){
            $where['tally_id'] = $condition['tally_id'] ; // 收入or支出
        }
        // 从DB获取
        $accountList = TallyProject::find()
            ->select('*')
            ->where($where)
            ->limit(100)
            ->asArray()
            ->all();
        return $accountList ;
    }

    /**
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getList($condition=[])
    {
        $where = [];
        if(ArrayHelper::getValue($condition,'type')){
            $where['type'] = $condition['type'] ; // 收入or支出
        }
        if(ArrayHelper::getValue($condition,'tally_id')){
            $where['tally_id'] = $condition['tally_id'] ; // 收入or支出
        }
        // 从DB获取
        $accountList = TallyProject::find()
            ->select('*')
            ->where($where);
        if(ArrayHelper::getValue($condition,'projects')){
            $accountList = $accountList->andFilterWhere(['in','id',ArrayHelper::getValue($condition,'projects')]);
        }
        if(ArrayHelper::getValue($condition,'titleLike')){
            $accountList = $accountList->andFilterWhere([
                'like','title',$condition['titleLike']
            ]);
        }
        $accountList = $accountList->limit(1000)
            ->asArray()
            ->all();

        return $accountList ;
    }

    /**
     * @param $id
     * @param int $uid
     * @return mixed
     */
    public static function getTitleById($id,$uid=0)
    {
        $where = [
            'id'    => $id,
        ];
        if($uid){
            $where['uid'] = $uid ; //uid可选
        }
        $account = TallyProject::find()
            ->select('*')
            ->where($where)
            ->limit(1000)
            ->asArray()
            ->limit(1)
            ->one();
        return $account ? $account['title'] : null;
    }

    /**
     * @param $formData
     * @return bool
     */
    public static function create($formData)
    {
        $model = new TallyProject();
        $model->load($formData,'');
        if ($model->validate() && $model->save()) {
            return $model->id ;
        }else{
            Yii::error('TallyStore insert error:'.current($model->getFirstErrors()).';post:'.Json::encode($formData,JSON_UNESCAPED_UNICODE));
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

    /**
     * @param $condition
     * @param $formData
     * @return bool
     */
    public static function update($condition,$formData)
    {
        $model = TallyProject::find()->where($condition)->one();
        if(!$model){
            self::setError('项目不存在');
            return false ;
        }
        $model->load($formData,'');
        if ($model->validate() && $model->save()) {
            return true ;
        }else{
            Yii::error('TallyProject insert error:'.current($model->getFirstErrors()).';post:'.Json::encode($formData,JSON_UNESCAPED_UNICODE));
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

    /**
     * @param $condition
     * @return bool
     * @throws
     */
    public static function delete($condition)
    {
        $model = TallyProject::find()->where($condition)->one();
        if(!$model){
            self::setError('项目不存在');
            return false ;
        }
        if ($model->delete()) {
            return true ;
        }else{
            self::setError(current($model->getFirstErrors()));
            return false;
        }
    }

}
